package com.itheima.zixi2.ZuoYe2;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        //创建一个数组用于存储学生数据
        STU s1 = new STU("张三", 18, "男", 180, 90.0);
        STU s2 = new STU("王五", 20, "女", 170, 89.0);
        STU s3 = new STU("小赵", 20, "男", 160, 92.0);
        STU s4 = new STU("小钱", 18, "女", 150, 86.0);
        STU s5 = new STU("小孙", 19, "男", 140, 99.0);
        STU[] S = {s1,s2,s3,s4,s5};
        //先根据学生的年纪对数组进行排序
        Arrays.sort(S, (STU o1, STU o2)-> o1.getAge()-o2.getAge());
        //对调整好后的数组遍历，如果年纪相等且前者成绩大于后者成绩，则交换两人的数据位置
        for (int i = 0; i < S.length-1; i++) {
            if(S[i].getAge() == S[i+1].getAge()&&Double.compare(S[i].getScore(),S[i+1].getScore())>0){
                STU temp;
                temp = S[i];
                S[i] = S[i+1];
                S[i+1] = temp;
            }
        }
        //遍历数组，打印结果
        for (int i = 0; i < S.length; i++) {
            System.out.println(S[i]);
        }
    }
}
